let regBtn = document.querySelector('#link_reg')
let logBtn = document.querySelector('#link_login')
let logDiv = document.querySelector('.login-box')
let regBox = document.querySelector('.reg-box')
let loginForm = document.querySelector('#form_login')
let regForm = document.querySelector('#form_reg')


regBtn.addEventListener('click', () => {
  //console.log(1);

  logDiv.style.display = "none"
  regBox.style.display = "block"
})
logBtn.addEventListener('click', () => {
  //console.log(1);
  logDiv.style.display = "block"
  regBox.style.display = "none"
})

loginForm.addEventListener('submit', (e) => {
  //console.log(1);
  e.preventDefault();
  let formData = new FormData(loginForm)
  let username = formData.get('username')
  let password = formData.get('password')

  if (username.length < 6 || password.length < 6) {
    alert('请输入正确格式的账号和密码')
  }else{

        axios({
      method: 'post',
      url: `/api/login`,
      // headers: {        
      //   'Content-Type': 'application/x-www-form-urlencoded',       
      // },
      data: {
        username,
        password
      },
       transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],

    }).then(res => {
     
      //alert(res.data.message+'请登录')
      if(res.data.status===0){
            //Location.href='../../index.html'
           
            console.log(res.data.token);
            localStorage.setItem('Token',res.data.token)
            window.location.href='./index.html'

      }else{
         alert(res.data.message)
      }


    }).catch(err => {
      console.log(err);

    })
  }
  
})

regForm.addEventListener('submit', (e) => {
  e.preventDefault();
  // console.log(2);
  let formData = new FormData(regForm)
  let username = formData.get('username')
  let password = formData.get('password')
  let rePwd = formData.get('repassword')

  if (username.length < 6 || password.length < 6) {
    alert('请输入正确格式的账号和密码')
  } else if (password !== rePwd) {
    alert('请确保两次密码输入一致')
  } else {
    axios({
      method: 'post',
      url: `/api/reguser`,
      // headers: {
      //   //'Content-Type': 'multipart/form-data'//含有附件的表单提交的Content-Type
      //   'Content-Type': 'application/x-www-form-urlencoded'//普通表单提交的Content-Type
      // },
      data: {
        username,
        password

      },
       transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],

    }).then(res => {
     
      //alert(res.data.message+'请登录')
      if(res.data.status===0){
             alert(res.data.message+'请登录')
             formData.set('username','')
              formData.set('password','')
              logBtn.click()

      }else{
         alert(res.data.message)
      }

    }).catch(err => {
      console.log(err);
    })
  }

})

function formSub(e) {

  e.preventDefault();


}